<!DOCTYPE html>
<html>
<head>
    <title>Wave Reflection and Transmission at an Interface</title>
</head>
<body>
<canvas id="physicsCanvas" width="400" height="300"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // --- Style and Font Settings ---
    ctx.lineWidth = 2;
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    const mainFont = '20px Times New Roman';
    const subFont = '14px Times New Roman';
    const axisFont = 'italic 18px Times New Roman';
    const captionFont = '16px Times New Roman';

    // --- Helper function for drawing arrows ---
    function drawArrow(x1, y1, x2, y2, headlen = 10) {
        const dx = x2 - x1;
        const dy = y2 - y1;
        const angle = Math.atan2(dy, dx);
        ctx.beginPath();
        // Draw line
        ctx.moveTo(x1, y1);
        ctx.lineTo(x2, y2);
        // Draw arrowhead
        ctx.moveTo(x2, y2);
        ctx.lineTo(x2 - headlen * Math.cos(angle - Math.PI / 6), y2 - headlen * Math.sin(angle - Math.PI / 6));
        ctx.moveTo(x2, y2);
        ctx.lineTo(x2 - headlen * Math.cos(angle + Math.PI / 6), y2 - headlen * Math.sin(angle + Math.PI / 6));
        ctx.stroke();
    }

    // --- Main Drawing ---

    // Define key coordinates
    const originX = 200;
    const axisY = 220;
    const interfaceTopY = 80;

    // Draw axes
    // z-axis (horizontal)
    drawArrow(40, axisY, 360, axisY);
    // Interface (vertical)
    ctx.beginPath();
    ctx.moveTo(originX, interfaceTopY);
    ctx.lineTo(originX, axisY);
    ctx.stroke();

    // Draw wave arrows
    const arrowY_A = 130;
    const arrowY_Ar = 150;
    const arrowY_B = 130; // Positioned similarly to A in the original
    const arrow_x_start_left = 60;
    const arrow_x_end_left = 180;
    const arrow_x_start_right = 220;
    const arrow_x_end_right = 340;

    // Incident wave A (pointing right)
    drawArrow(arrow_x_start_left, arrowY_A, arrow_x_end_left, arrowY_A);

    // Reflected wave Ar (pointing left)
    drawArrow(arrow_x_end_left, arrowY_Ar, arrow_x_start_left, arrowY_Ar);

    // Transmitted wave B (pointing right)
    drawArrow(arrow_x_start_right, arrowY_B, arrow_x_end_right, arrowY_B);

    // --- Draw Labels ---

    // Region labels
    ctx.font = mainFont;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText('(1)', 120, 90);
    ctx.fillText('(2)', 280, 90);

    // Wave labels
    ctx.textAlign = 'left';
    // Label 'A'
    ctx.fillText('A', arrow_x_end_left + 8, arrowY_A);
    // Label 'B'
    ctx.fillText('B', arrow_x_end_right + 8, arrowY_B);
    
    // Label 'Ar' (with subscript)
    // The label is near the arrowhead of the reflected wave.
    ctx.textAlign = 'right';
    ctx.fillText('A', arrow_x_start_left - 8, arrowY_Ar);
    ctx.font = subFont;
    ctx.textAlign = 'left';
    ctx.fillText('r', arrow_x_start_left - 8, arrowY_Ar + 5);


    // Axis labels
    ctx.font = mainFont;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'top';
    ctx.fillText('O', originX, axisY + 4);
    
    ctx.font = axisFont;
    ctx.textAlign = 'left';
    ctx.textBaseline = 'middle';
    ctx.fillText('z', 365, axisY);

    // Figure caption
    ctx.font = captionFont;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';
    ctx.fillText('Fig. 1.223.', canvas.width / 2, canvas.height - 10);

</script>
</body>
</html>